﻿@model OpenAccountInfo
@{
    var documents = (List<Document>)ViewBag.documents;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <link href="/css/style.css" rel="stylesheet" type="text/css" />
    <link href="/css/select.css" rel="stylesheet" type="text/css" />
    <link href="/css/openuserstyle.css" rel="stylesheet" type="text/css" />
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/mustache.js"></script>
    <script type="text/javascript" src="/js/jquery.idTabs.min.js"></script>
    <script type="text/javascript" src="/js/select-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function (e) {
            $(".select1").uedSelect({
                width: 345
            });
            $(".select2").uedSelect({
                width: 167
            });
            $(".select3").uedSelect({
                width: 150
            });
            var customerId = $('#btnSearch').parents("ul").find("[name='customerIdX']").val();
            if (customerId) {
                Search();
            }
        });

        ///搜索
        function Search(obj) {
            $ul = $('#btnSearch').parents("ul");
            var customerId = $ul.find("[name='customerIdX']").val();
            $.post('/user/FindByCustomerId', { customerId: customerId }, function (data) {
                if (data.Succeed) {
                    $ul.find("[name='customerName']").val(data.Obj.Name);
                    $ul.find("[name='area']").val(data.Obj.FirstArea + "-" + data.Obj.SecondArea);
                    $ul.find("[name='detailAddress']").val(data.Obj.DetailAddress);
                    $('#CustomerId').val(data.Obj.Id);
                }
                else {
                    window.alert("指定编号的用户不存在！");
                }
            }, 'json')
        }

        function changeDocument(obj) {
            var jsonModel = $(obj).find("option:selected").attr('jsonModel');
            var document = $.parseJSON(jsonModel);
            $(obj).parents("ul").find("[name='Unit']").val(document.DocumentUnit);
            $(obj).parents("ul").find("[name='Price']").val(document.DocumentPrice);
        }

        ///添加材料
        function AddDocument(o) {
            $obj = $(o).parents('ul');
            var doc = {
                DocumentName: $obj.find('option:selected').text(),
                DocumentUnit: $obj.find('[name="Unit"]').val(),
                DocumentPrice: $obj.find('[name="Price"]').val(),
                DocumentNum: $obj.find('[name="Num"]').val(),
                DocumentDesc: $obj.find('[name="Description"]').val()
            };
            doc.json = JSON.stringify(doc);
            if (!doc.DocumentName || doc.DocumentName == undefined) { alert("材料名称未选择"); return; }
            if (!doc.DocumentUnit || doc.DocumentUnit == undefined) { alert("材料单位未输入"); return; }
            if (!doc.DocumentPrice || doc.DocumentPrice == undefined) { alert("材料价格未输入"); return; }
            if (!doc.DocumentNum || doc.DocumentNum == undefined) { alert("材料数量未输入"); return; }
            // if (!doc.Description || doc.Description == undefined) { alert("材料名称未选择"); return; }
            var totalPrice = (parseFloat(doc.DocumentNum) * parseFloat(doc.DocumentPrice)).toFixed(2);
            doc.TotalPrice = totalPrice;
            var model = { documents: [doc] };
            var trHtml = Mustache.render($('#documentTemplate').html(), model);
            $('#tbody1').append(trHtml);
            getCharge();
            getShouldPay();
            GetRealCharge();

        }

        function getCharge() {
            //计算总价
            var allPrice = 0;
            $.each($('#tbody1 tr'), function (i, obj) {
                var tprice = $(obj).find('td').eq(4).text();
                allPrice += parseFloat(tprice);
            })
            $('#tfoot1 tr:first').find('td').eq(1).text(allPrice.toFixed(2));
            $('#DocumentCharge').val(allPrice.toFixed(2));
        }


        function getShouldPay() {
            var OpenAccountCharge = $('#OpenAccountCharge').val();
            var DocumentCharge = $('#DocumentCharge').val();
            var WorkTimeCharge = $('#WorkTimeCharge').val();
            var OtherCharge = $('#OtherCharge').val();


            var m1 = ($.isNumeric(OpenAccountCharge) ? parseFloat(OpenAccountCharge) : 0);
            var m2 = ($.isNumeric(DocumentCharge) ? parseFloat(DocumentCharge) : 0);
            var m3 = ($.isNumeric(WorkTimeCharge) ? parseFloat(WorkTimeCharge) : 0);
            var m4 = ($.isNumeric(OtherCharge) ? parseFloat(OtherCharge) : 0);
            $('#ShouldGetCharge').val((m1 + m2 + m3 + m4).toFixed(2));
            GetRealCharge();
        }

        function GetRealCharge() {
            var x = parseFloat($('#ShouldGetCharge').val());
            var v = $('#RealyGetCharge').val();
            if ($.isNumeric(v)) {
                var y = (parseFloat(v) - x);
                $('#RestoreCharge').val(y);
            }
        }


        function Check() {
            var strobj = [];
            //组织json
            $.each($('#tbody1 tr'), function (i, obj) {
                var jsonModel = $.parseJSON($(obj).attr('jsonModel'));
                strobj.push(jsonModel);
            })
            $('#documentsJson').val(JSON.stringify(strobj));
            return true;
        }


    </script>

    <script type="text/template" id="documentTemplate">
        {{#documents}}
        <tr jsonModel="{{json}}">
            <td>{{DocumentName}}</td>
            <td>{{DocumentUnit}}</td>
            <td>{{DocumentPrice}} </td>
            <td>{{DocumentNum}}</td>
            <td>{{TotalPrice}} </td>
            <td>{{DocumentDesc}}</td>
            <td><input name="" type="button" class="scbtn" value="删除材料" onclick="$(this).parents('tr').remove(); getCharge(); getShouldPay();" /></td>
        </tr>
        {{/documents}}
    </script>

</head>

<body>

    <div class="place">
        <span>位置：</span>
        <ul class="placeul">
            <li><a href="#">首页</a></li>
            <li><a href="#">用户管理</a></li>
            <li><a href="#">开户费用添加</a></li>
        </ul>
    </div>
    @using (Html.BeginForm("Add_OpenAccountCharge", "User", FormMethod.Post))
    {
        <div class="formbodycharge">
            <div class="userinfocharge">
                用户信息
            </div>
            <div id="tab2" class="tabsoncharge">
                <ul class="seachformcharge">
                    <li><label>用户编号:</label><input type="text" value="@(ViewBag.customerId??"")" class="scinput" name="customerIdX" /></li>
                    <li><label>用户姓名:</label><input type="text" value="" class="scinput" name="customerName" /></li>
                    <li><label>区域地址:</label><input type="text" value="" class="scinput" name="area" disabled="disabled" style="background:#EFEFEF" /></li>
                    <li><label>详细地址: </label><input type="text" value="" class="scinput" name="detailAddress" disabled="disabled" style="background:#EFEFEF" /></li>
                    <li><label>&nbsp;</label><input name="" id="btnSearch" type="button" onclick="Search(this)" class="scbtn" value="查询" /></li>
                </ul>
            </div>
            <div class="userinfoopen">
                资料信息
            </div>
            <div id="tab2" class="tabsoncharge">
                <ul class="seachformcharge">
                    <li><label>开户费用:</label><input type="text" value="@(Model?.OpenAccountCharge)" class="scinput" name="OpenAccountCharge" id="OpenAccountCharge" onkeyup="getShouldPay(this)" /></li>
                    <li><label>材料费用:</label><input type="text" value="@(Model?.DocumentCharge)" class="scinput" id="DocumentCharge" name="DocumentCharge" onchange="getShouldPay(this)" onkeyup="getShouldPay(this)" /></li>
                    <li><label>工时费用:</label><input type="text" value="@(Model?.WorkTimeCharge)" class="scinput" id="WorkTimeCharge" name="WorkTimeCharge" onkeyup="getShouldPay(this)" /></li>
                    <li><label>其它费用:</label><input type="text" value="@(Model?.OtherCharge)" class="scinput" id="OtherCharge" name="OtherCharge" onkeyup="getShouldPay(this)" /></li>
                </ul>
            </div>
            <div class="userinfomaterial">
                材料信息
            </div>
            <div id="tab2" class="tabsoncharge">
                <ul class="seachformmaterialinfo">
                    <li>
                        <label>材料名称:</label>
                        <div class="vocation">
                            <select class="select3" onchange="changeDocument(this)">
                                <option value="">选择材料</option>
                                @foreach (var item in documents)
                                {
                                    <option jsonModel="@(JsonConvert.SerializeObject(item, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }))" value="@(item.Id)">@(item.DocumentName)</option>
                                }
                            </select>
                        </div>
                    </li>
                    <li><label>材料单位:</label><input type="text" value="" class="scinput" name="Unit" /></li>
                    <li><label>材料单价:</label><input type="text" value="" class="scinput" name="Price" /></li>
                    <li><label>材料数量: </label><input type="text" value="" class="scinput" name="Num" /></li>

                    <li><label>&nbsp;</label><input name="" type="button" class="scbtn" value="添加材料" onclick="AddDocument(this)" /></li>
                    <li><label>材料备注: </label><input type="text" value="" class="scinputmaterialinfo" name="Description" /></li>

                </ul>
                <table class="tablelistcharge">
                    <thead>
                        <tr>
                            <th>材料名称</th>
                            <th>材料单位</th>
                            <th>材料单价</th>
                            <th>材料数量</th>
                            <th>材料金额</th>
                            <th>材料备注</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody id="tbody1">
                        @foreach (var item in Model?.OpenAccountDocuments)
                        {
                            <tr jsonModel="@(JsonConvert.SerializeObject(item,new JsonSerializerSettings{ReferenceLoopHandling = ReferenceLoopHandling.Ignore}))">
                                <td>@(item.DocumentName)</td>
                                <td>@(item.DocumentUnit)</td>
                                <td>@(item.DocumentPrice)</td>
                                <td>@(item.DocumentNum)</td>
                                <td>@((item.DocumentNum * item.DocumentPrice).ToString("f2"))</td>
                                <td>@(item.DocumentDesc)</td>
                                <td><input name="" type="button" class="scbtn" value="删除材料" onclick="$(this).parents('tr').remove(); getCharge(); getShouldPay();" /></td>
                            </tr>
                        }
                    </tbody>
                    <tfoot id="tfoot1">
                        <tr>
                            <td>总计金额</td>
                            <td colspan="6">@(Model.OpenAccountDocuments.Sum(item => item.DocumentNum * item.DocumentPrice).ToString("f2"))</td>

                        </tr>
                    </tfoot>
                </table>

            </div>


            <div id="tab2" class="tabson">
                <ul class="seachformchargesf">
                    <li><label>应收金额:</label><font color="ff0000"><input type="text" id="ShouldGetCharge" name="ShouldGetCharge" value="@(Model?.ShouldGetCharge)" class="scinputchargesfred" readonly=readonly style="background:#EFEFEF" /></font></li>
                    <li><label>收款金额: </label><input type="text" value="@(Model?.RealyGetCharge)" class="scinputchargesfgreen" id="RealyGetCharge" name="RealyGetCharge" onkeyup="GetRealCharge(this)" /></li>
                    <li>
                        <label>本次找零:</label>
                        <input type="text" value="@(Model?.RestoreCharge)" class="scinputchargesfred" name="RestoreCharge" id="RestoreCharge" readonly=readonly style="background:#EFEFEF" />
                    </li>
                    <li><label>收费人员:</label><input type="text" value="@(((Kongt.Web.Models.LoginModel)ViewBag.currentUser).UserName)" class="scinputchargesf" readonly=readonly style="background:#EFEFEF" /></li>
                    <li><label>&nbsp;</label><input type="submit" onclick="return Check();" class="scbtnchargesf" value="开户" /></li>
                </ul>
                @Html.Hidden("CustomerId", Model.CustomerId==0?"": Model.CustomerId.ToString())
                @Html.Hidden("documentsJson", "")
                @Html.Hidden("Id",Model.Id==0?"0":Model.Id.ToString())
            </div>
        </div>
    }
</body>

</html>
